﻿using FreeSql;
using FreeSql.Internal.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace OkTool.Db
{
    public static class FreeSqlUtil
    {


        private static readonly object _locker = new object();

        private static IFreeSql _instance { get; set; }

        /// <summary>
        /// 数据库类型
        /// </summary>
        public static DataType DataType;

        /// <summary>
        /// 连接字符串
        /// </summary>
        public static string ConnectionString;


        /// <summary>
        /// 实例
        /// </summary>
        /// <returns></returns>
        public static IFreeSql Instance()
        {
            if (_instance == null)
            {
                lock (_locker)
                {
                    if (_instance == null)
                    {
                        _instance = new FreeSqlBuilder().UseConnectionString(DataType, ConnectionString).Build();
                    }
                }
            }
            return _instance;
        }

        /// <summary>
        /// 过滤表全部字段,字段标记特性FreeSqlFilterAttribute才会过滤
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="select"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static ISelect<T1> FilterAllField<T1>(this ISelect<T1> select, string keyword)
        {
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                DynamicFilterInfo dynamicFilterInfo = new DynamicFilterInfo();
                dynamicFilterInfo.Logic = DynamicFilterLogic.Or;
                dynamicFilterInfo.Filters = new List<DynamicFilterInfo>();

                // 获取实体类型
                var entityType = typeof(T1);
                // 遍历实体属性
                foreach (var property in entityType.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword
                        });
                    }
                       
                }
                return select.WhereDynamicFilter(dynamicFilterInfo);
            }
            else
            {
                return select;
            }
        }

        /// <summary>
        /// 过滤表全部字段,字段标记特性FreeSqlFilterAttribute才会过滤
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="select"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static ISelect<T1, T2> FilterAllField<T1, T2>(this ISelect<T1, T2> select, string keyword) where T2 : class
        {
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                DynamicFilterInfo dynamicFilterInfo = new DynamicFilterInfo();
                dynamicFilterInfo.Logic = DynamicFilterLogic.Or;
                dynamicFilterInfo.Filters = new List<DynamicFilterInfo>();
                // 获取实体类型
                var entityType = typeof(T1);
                // 遍历实体属性
                foreach (var property in entityType.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                       
                }
                //FreeSqlFilterAttribute
                var entity2Type = typeof(T2);

                foreach (var property in entity2Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "b." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }

                }
                return select.WhereDynamicFilter(dynamicFilterInfo);
            }
            else
            {
                return select;
            }
        }

        /// <summary>
        /// 过滤表全部字段,字段标记特性FreeSqlFilterAttribute才会过滤
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <param name="select"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static ISelect<T1, T2, T3> FilterAllField<T1, T2, T3>(this ISelect<T1, T2, T3> select, string keyword) where T2 : class where T3 : class
        {
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                DynamicFilterInfo dynamicFilterInfo = new DynamicFilterInfo();
                dynamicFilterInfo.Logic = DynamicFilterLogic.Or;
                dynamicFilterInfo.Filters = new List<DynamicFilterInfo>();
                // 获取实体类型
                var entityType = typeof(T1);
                // 遍历实体属性
                foreach (var property in entityType.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                       
                }
                var entity2Type = typeof(T2);
                // 遍历实体属性
                foreach (var property in entity2Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "b." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }

                }
                var entity3Type = typeof(T3);
                // 遍历实体属性
                foreach (var property in entity3Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "c." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }

                return select.WhereDynamicFilter(dynamicFilterInfo);
            }
            else
            {
                return select;
            }
        }
        /// <summary>
        /// 过滤表全部字段,字段标记特性FreeSqlFilterAttribute才会过滤
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <typeparam name="T4"></typeparam>
        /// <param name="select"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static ISelect<T1, T2, T3, T4> FilterAllField<T1, T2, T3, T4>(this ISelect<T1, T2, T3, T4> select, string keyword) where T2 : class where T3 : class where T4 : class
        {
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                DynamicFilterInfo dynamicFilterInfo = new DynamicFilterInfo();
                dynamicFilterInfo.Logic = DynamicFilterLogic.Or;
                dynamicFilterInfo.Filters = new List<DynamicFilterInfo>();
                // 获取实体类型
                var entityType = typeof(T1);
                // 遍历实体属性
                foreach (var property in entityType.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                        
                }
                var entity2Type = typeof(T2);
                // 遍历实体属性
                foreach (var property in entity2Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "b." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }

                }
                var entity3Type = typeof(T3);
                // 遍历实体属性
                foreach (var property in entity3Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "c." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity4Type = typeof(T4);
                // 遍历实体属性
                foreach (var property in entity4Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "d." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                return select.WhereDynamicFilter(dynamicFilterInfo);
            }
            else
            {
                return select;
            }
        }

        /// <summary>
        /// 过滤表全部字段,字段标记特性FreeSqlFilterAttribute才会过滤
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <typeparam name="T4"></typeparam>
        /// <typeparam name="T5"></typeparam>
        /// <param name="select"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static ISelect<T1, T2, T3, T4, T5> FilterAllField<T1, T2, T3, T4, T5>(this ISelect<T1, T2, T3, T4, T5> select, string keyword) where T2 : class where T3 : class where T4 : class where T5 : class
        {
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                DynamicFilterInfo dynamicFilterInfo = new DynamicFilterInfo();
                dynamicFilterInfo.Logic = DynamicFilterLogic.Or;
                dynamicFilterInfo.Filters = new List<DynamicFilterInfo>();
                // 获取实体类型
                var entityType = typeof(T1);
                // 遍历实体属性
                foreach (var property in entityType.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                       
                }
                var entity2Type = typeof(T2);
                // 遍历实体属性
                foreach (var property in entity2Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity3Type = typeof(T3);
                // 遍历实体属性
                foreach (var property in entity3Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity4Type = typeof(T4);
                // 遍历实体属性
                foreach (var property in entity4Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity5Type = typeof(T5);
                // 遍历实体属性
                foreach (var property in entity5Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                return select.WhereDynamicFilter(dynamicFilterInfo);
            }
            else
            {
                return select;
            }
        }


        /// <summary>
        /// 过滤表全部字段,字段标记特性FreeSqlFilterAttribute才会过滤
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <typeparam name="T4"></typeparam>
        /// <typeparam name="T5"></typeparam>
        /// <typeparam name="T6"></typeparam>
        /// <param name="select"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static ISelect<T1, T2, T3, T4, T5, T6> FilterAllField<T1, T2, T3, T4, T5, T6>(this ISelect<T1, T2, T3, T4, T5, T6> select, string keyword) where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class
        {
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                DynamicFilterInfo dynamicFilterInfo = new DynamicFilterInfo();
                dynamicFilterInfo.Logic = DynamicFilterLogic.Or;
                dynamicFilterInfo.Filters = new List<DynamicFilterInfo>();
                // 获取实体类型
                var entityType = typeof(T1);
                // 遍历实体属性
                foreach (var property in entityType.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                       
                }
                var entity2Type = typeof(T2);
                // 遍历实体属性
                foreach (var property in entity2Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "b." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity3Type = typeof(T3);
                // 遍历实体属性
                foreach (var property in entity3Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "c." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity4Type = typeof(T4);
                // 遍历实体属性
                foreach (var property in entity4Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "d." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity5Type = typeof(T5);
                // 遍历实体属性
                foreach (var property in entity5Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "e." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity6Type = typeof(T6);
                // 遍历实体属性
                foreach (var property in entity6Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "f." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                return select.WhereDynamicFilter(dynamicFilterInfo);
            }
            else
            {
                return select;
            }
        }

        /// <summary>
        /// 过滤表全部字段,字段标记特性FreeSqlFilterAttribute才会过滤
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <typeparam name="T4"></typeparam>
        /// <typeparam name="T5"></typeparam>
        /// <typeparam name="T6"></typeparam>
        /// <typeparam name="T7"></typeparam>
        /// <typeparam name="T8"></typeparam>
        /// <param name="select"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static ISelect<T1, T2, T3, T4, T5, T6, T7, T8> FilterAllField<T1, T2, T3, T4, T5, T6, T7, T8>(this ISelect<T1, T2, T3, T4, T5, T6, T7, T8> select, string keyword) where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class
        {
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                DynamicFilterInfo dynamicFilterInfo = new DynamicFilterInfo();
                dynamicFilterInfo.Logic = DynamicFilterLogic.Or;
                dynamicFilterInfo.Filters = new List<DynamicFilterInfo>();
                // 获取实体类型
                var entityType = typeof(T1);
                // 遍历实体属性
                foreach (var property in entityType.GetProperties())
                {
                    dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                    {
                        Field = property.Name,
                        Operator = DynamicFilterOperator.Contains,
                        Value = keyword,
                        Logic = DynamicFilterLogic.Or
                    });
                }
                var entity2Type = typeof(T2);
                // 遍历实体属性
                foreach (var property in entity2Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "b." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity3Type = typeof(T3);
                // 遍历实体属性
                foreach (var property in entity3Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "c." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity4Type = typeof(T4);
                // 遍历实体属性
                foreach (var property in entity4Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "d." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity5Type = typeof(T5);
                // 遍历实体属性
                foreach (var property in entity5Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "e." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity6Type = typeof(T6);
                // 遍历实体属性
                foreach (var property in entity6Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "f." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity7Type = typeof(T7);
                // 遍历实体属性
                foreach (var property in entity7Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "g." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                var entity8Type = typeof(T8);
                // 遍历实体属性
                foreach (var property in entity8Type.GetProperties())
                {
                    if (property.GetCustomAttribute<FreeSqlFilterAttribute>() != null)
                    {
                        dynamicFilterInfo.Filters.Add(new DynamicFilterInfo
                        {
                            Field = "h." + property.Name,
                            Operator = DynamicFilterOperator.Contains,
                            Value = keyword,
                            Logic = DynamicFilterLogic.Or
                        });
                    }
                }
                return select.WhereDynamicFilter(dynamicFilterInfo);
            }
            else
            {
                return select;
            }
        }
    }
}
